home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / graphics / fgfish20.zip / FISHTANK.C < prev    next >
Text File  |  1995-02-12  |  10KB  |  322 lines

  1. /**********************************************************************\
  2.  
  3. FISHTANK.C -- This program demonstrates multi-object non-destructive
  4. animation. The coral background is displayed on page 2 and copied to
  5. page 0, the visual page. A packed pixel run file containing the 6 fish
  6. is displayed on page 1, and then fg_getimage is used to load the fish
  7. into the fish bitmaps.
  8.  
  9. To make the fish move, the background is copied to page 1 and the fish
  10. are put over the background using fg_clpimage and fg_flpimage. The
  11. fish are clipped at the edge of the screen. Page 1 is then copied to
  12. page 0 using fg_copypage. This process is repeated in a loop.
  13.  
  14. To compile this program and link it with Fastgraph version 4.0:
  15.  
  16.    Borland C++:
  17.       BCC -ms FISHTANK.C FGS.LIB
  18.  
  19.    Microsoft C/C++ and Visual C++:
  20.       CL /AS FISHTANK.C /link FGS
  21.  
  22.    Microsoft QuickC:
  23.       QCL /AS FISHTANK.C /link FGS
  24.  
  25.    Microsoft Visual C++ 32-bit Edition with Phar Lap TNT extender:
  26.       CL FISHTANK.C /link /stub:\TNT\BIN\GOTNT.EXE FG32VC.LIB
  27.  
  28.    Power C:
  29.       PC /ms FISHTANK
  30.       PCL FISHTANK ;FGS
  31.  
  32.    Turbo C and Turbo C++:
  33.       TCC -ms FISHTANK.C FGS.LIB
  34.  
  35.    Watcom C/C++ (16 bits):
  36.       WCL /ms FISHTANK.C FGS.LIB
  37.  
  38.    Watcom C/C++ (32 bits) with Rational Systems DOS/4GW extender:
  39.       WCL386 /l=dos4g FISHTANK.C FG32.LIB FG32DPMI.LIB
  40.  
  41.    Zortech C++:
  42.       ZTC -ms FISHTANK.C FGS.LIB
  43.  
  44. This program also can be linked with Fastgraph/Light 4.0 if you replace
  45. the FGS library references with FGLS.
  46.  
  47. For more examples of animation using Fastgraph, or for an evaluation
  48. copy of Fastgraph/Light, call DDBBS at (702) 796-7134. For Fastgraph
  49. voice support, call Ted Gruber Software at (702) 735-1980.
  50.  
  51. \**********************************************************************/
  52.  
  53. #include <fastgraf.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56.  
  57. void main(void);
  58. void get_fish(void);
  59. void go_fish(void);
  60. int  irandom(int min,int max);
  61. void put_fish(int fish_no,int x,int y,int fish_dir);
  62. void random_init(void);
  63.  
  64. #define MAX(x,y) ((x) > (y)) ? (x) : (y)
  65. #define MIN(x,y) ((x) < (y)) ? (x) : (y)
  66.  
  67. #define NFISH 11
  68.  
  69. int seed;
  70.  
  71. /*** fish bitmaps ***/
  72.  
  73. char fish1[700];
  74. char fish2[1026];
  75. char fish3[884];
  76. char fish4[840];
  77. char fish5[682];
  78. char fish6[1224];
  79.  
  80. /**********************************************************************\
  81. *                                                                      *
  82. *                                 main                                 *
  83. *                                                                      *
  84. \**********************************************************************/
  85.  
  86. int colors[] = {0,1,2,3,4,5,6,7,16,0,18,19,20,21,22,23};
  87.  
  88. void main()
  89. {
  90.    int old_mode;
  91.  
  92.    /* in case we're compiling for protected mode */
  93.  
  94.    fg_initpm();
  95.  
  96.    /* make sure the system supports video mode 13 with 4 pages */
  97.  
  98.    if (fg_testmode(13,4) == 0)
  99.    {
  100.       printf("\n");
  101.       printf("This program requires an EGA or VGA card\n");
  102.       printf("with at least 128k. If an EGA card is\n");
  103.       printf("present, it must be the active adapter.\n");
  104.       exit(0);
  105.    }
  106.  
  107.    /* initialize the video environment */
  108.  
  109.    old_mode = fg_getmode();
  110.    fg_setmode(13);
  111.    fg_palettes(colors);
  112.    random_init();
  113.  
  114.    /* get the coral background from a file and put it on page 2 */
  115.  
  116.    fg_setpage(2);
  117.    fg_move(0,199);
  118.    fg_showppr("CORAL.PPR",320);
  119.  
  120.    /* copy the background from page 2 to page 0, the visual page */
  121.  
  122.    fg_copypage(2,0);
  123.  
  124.    /* get the fish */
  125.  
  126.    get_fish();
  127.  
  128.    /* make the fish go */
  129.  
  130.    go_fish();
  131.  
  132.    /* restore the original video state */
  133.  
  134.    fg_setmode(old_mode);
  135.    fg_reset();
  136. }
  137.  
  138. /**********************************************************************\
  139. *                                                                      *
  140. *            get_fish -- fill up the fish bitmap arrays                *
  141. *                                                                      *
  142. \**********************************************************************/
  143.  
  144. int fish_x1[]  = {0,   64,128,200,  0, 80}; /* location of fish x & y */
  145. int fish_y1[]  = {199,199,199,199,150,150};
  146. int width[]    = { 28, 27, 34, 28, 31, 34}; /* size of fish: width */
  147. int height[]   = { 25, 38, 26, 30, 22, 36}; /* size of fish: height */
  148.  
  149. char *fishes[] = {fish1, /* for convenience, an array of pointers to fishes */
  150.                   fish2,
  151.                   fish3,
  152.                   fish4,
  153.                   fish5,
  154.                   fish6};
  155.  
  156. void get_fish()
  157. {
  158.    register int fish_num;
  159.  
  160.    /* get the fish from a file and put them on page 1 */
  161.  
  162.    fg_setpage(1);
  163.    fg_move(0,199);
  164.    fg_showppr("FISH.PPR",320);
  165.  
  166.    /* build the fish bitmaps */
  167.  
  168.    for (fish_num = 0; fish_num < 6; fish_num++)
  169.    {
  170.       fg_move(fish_x1[fish_num],fish_y1[fish_num]);
  171.       fg_getimage(fishes[fish_num],width[fish_num],height[fish_num]);
  172.    }
  173. }
  174.  
  175. /**********************************************************************\
  176. *                                                                      *
  177. *             go_fish -- make the fish swim around                     *
  178. *                                                                      *
  179. \**********************************************************************/
  180.  
  181. /*  There are 11 fish total, and 6 different kinds of fish. These
  182. *   arrays keep track of what kind of fish each fish is, and how each
  183. *   fish moves:
  184. *
  185. *   fish[]   -- which fish bitmap applies to this fish?
  186. *   x[]      -- starting x coordinate
  187. *   y[]      -- starting y coordinate
  188. *
  189. *   xmin[]   -- how far left (off screen) the fish can go
  190. *   xmax[]   -- how far right (off screen) the fish can go
  191. *   xinc[]   -- how fast the fish goes left and right
  192. *   dir[]    -- starting direction for each fish
  193. *
  194. *   ymin[]   -- how far up this fish can go
  195. *   ymax[]   -- how far down this fish can go
  196. *   yinc[]   -- how fast the fish moves up or down
  197. *   yturn[]  -- how long fish can go in the vertical direction
  198. *               before stopping or turning around
  199. *   ycount[] -- counter to compare to yturn
  200. */
  201.  
  202. int fish[]   = {   1,   1,   2,   3,   3,   0,   0,   5,   4,   2,   3};
  203. int x[]      = {-100,-150,-450,-140,-200, 520, 620,-800, 800, 800,-300};
  204. int y[]      = {  40,  60, 150,  80,  70, 190, 180, 100,  30, 130,  92};
  205.  
  206. int xmin[]   = {-300,-300,-800,-200,-200,-200,-300,-900,-900,-900,-400};
  207. int xmax[]   = { 600, 600,1100,1000,1000, 750, 800,1200,1400,1200, 900};
  208. int xinc[]   = {   2,   2,   8,   5,   5,  -3,  -3,   7,  -8,  -9,   6};
  209. int dir[]    = {   0,   0,   0,   0,   0,   1,   1,   0,   1,   1,   0};
  210.  
  211. int ymin[]   = {  40,  60, 120,  70,  60, 160, 160,  80,  30, 110,  72};
  212. int ymax[]   = {  80, 100, 170, 110, 100, 199, 199, 120,  70, 150, 122};
  213. int yturn[]  = {  50,  30,  10,  30,  20,  10,  10,  10,  30,   20, 10};
  214. int ycount[] = {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0};
  215. int yinc[]   = {   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0};
  216.  
  217. void go_fish()
  218. {
  219.    register int i;
  220.    unsigned char key, aux;
  221.  
  222.    /* make the fish swim around */
  223.  
  224.    do
  225.    {
  226.       /* copy the background from page 2 to page 1 */
  227.  
  228.       fg_copypage(2,1);
  229.  
  230.       /* put all the fish on the background */
  231.  
  232.       for (i = 0; i < NFISH; i++)
  233.       {
  234.          fg_setpage(1);
  235.          ycount[i]++;
  236.          if (ycount[i] > yturn[i])
  237.          {
  238.             ycount[i] = 0;
  239.             yinc[i] = irandom(-1,1);
  240.          }
  241.          y[i] += yinc[i];
  242.          y[i] = MIN(ymax[i],MAX(y[i],ymin[i]));
  243.  
  244.          if (x[i] >= 0 && x[i] < 320)
  245.          {
  246.             put_fish(fish[i],x[i],y[i],dir[i]);
  247.          }
  248.          else if (x[i] < 0 && x[i] > -72)
  249.          {
  250.             fg_transfer(0,71,0,199,104,199,1,3);
  251.             fg_setpage(3);
  252.             put_fish(fish[i],x[i]+104,y[i],dir[i]);
  253.             fg_transfer(104,175,0,199,0,199,3,1);
  254.          }
  255.          x[i] += xinc[i];
  256.          if (x[i] <= xmin[i] || x[i] >= xmax[i])
  257.          {
  258.             xinc[i] = -xinc[i];
  259.             dir[i] = 1 - dir[i];
  260.          }
  261.       }
  262.  
  263.       /* copy page 1 to page 0 */
  264.  
  265.       fg_setpage(0);
  266.       fg_copypage(1,0);
  267.  
  268.       /* intercept a keystroke, if it is escape exit the program */
  269.  
  270.       fg_intkey(&key,&aux);
  271.    }
  272.    while (key != 27);
  273. }
  274.  
  275. /**********************************************************************\
  276. *                                                                      *
  277. *                irandom -- random number generator                    *
  278. *                                                                      *
  279. \**********************************************************************/
  280.  
  281. irandom(min,max)
  282. int min,max;
  283. {
  284.    int temp;
  285.  
  286.    temp = seed ^ (seed >> 7);
  287.    seed = ((temp << 8) ^ temp) & 0x7FFF;
  288.    return((seed % (max-min+1)) + min);
  289. }
  290.  
  291. /**********************************************************************\
  292. *                                                                      *
  293. *      put_fish -- draw one of the six fish anywhere you want          *
  294. *                                                                      *
  295. \**********************************************************************/
  296.  
  297. void put_fish(fish_num,x,y,fish_dir)
  298. int fish_num,x,y,fish_dir;
  299. {
  300.    /* move to position where the fish will appear */
  301.  
  302.    fg_move(x,y);
  303.  
  304.    /* draw a left- or right-facing fish, depending on fish_dir */
  305.  
  306.    if (fish_dir == 0)
  307.       fg_flpimage(fishes[fish_num],width[fish_num],height[fish_num]);
  308.    else
  309.       fg_clpimage(fishes[fish_num],width[fish_num],height[fish_num]);
  310. }
  311.  
  312. /**********************************************************************\
  313. *                                                                      *
  314. *      random_init -- get a seed for the random number generator       *
  315. *                                                                      *
  316. \**********************************************************************/
  317.  
  318. void random_init()
  319. {
  320.    seed = (int)(fg_getclock() & 0x7FFF);
  321. }
  322.